package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.TbRepairplanlist;
import com.ruoyi.tb.lmt.mapper.TbRepairplanlistMapper;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.javassist.expr.Cast;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbCarparkingBo;
import com.ruoyi.tb.lmt.domain.vo.TbCarparkingVo;
import com.ruoyi.tb.lmt.domain.TbCarparking;
import com.ruoyi.tb.lmt.mapper.TbCarparkingMapper;
import com.ruoyi.tb.lmt.service.ITbCarparkingService;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 存车场信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-21
 */
@RequiredArgsConstructor
@Service
public class TbCarparkingServiceImpl implements ITbCarparkingService {

    private final TbCarparkingMapper baseMapper;

    @Autowired
    private TbRepairplanlistMapper tbRepairplanlistMapper;

    /**
     * 查询存车场信息
     */
    @Override
    public TbCarparkingVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询存车场信息列表
     */
    @Override
    public TableDataInfo<TbCarparkingVo> queryPageList(TbCarparkingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbCarparking> lqw = buildQueryWrapper(bo);
        Page<TbCarparkingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询存车场信息列表
     */
    @Override
    public List<TbCarparkingVo> queryList(TbCarparkingBo bo) {
        LambdaQueryWrapper<TbCarparking> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbCarparking> buildQueryWrapper(TbCarparkingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbCarparking> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTrackId() != null, TbCarparking::getTrackId, bo.getTrackId());
        lqw.eq(bo.getRequireId() != null, TbCarparking::getRequireId, bo.getRequireId());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtType()), TbCarparking::getLmtType, bo.getLmtType());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtNumber()), TbCarparking::getLmtNumber, bo.getLmtNumber());
        lqw.eq(bo.getLmtRepairId() != null, TbCarparking::getLmtRepairId, bo.getLmtRepairId());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairType()), TbCarparking::getRepairType, bo.getRepairType());
        lqw.like(StringUtils.isNotBlank(bo.getPlanName()), TbCarparking::getPlanName, bo.getPlanName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanStatus()), TbCarparking::getPlanStatus, bo.getPlanStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkGroup()), TbCarparking::getWorkGroup, bo.getWorkGroup());
        lqw.eq(bo.getWorkGroupId() != null, TbCarparking::getWorkGroupId, bo.getWorkGroupId());
        lqw.like(StringUtils.isNotBlank(bo.getTrackName()), TbCarparking::getTrackName, bo.getTrackName());
        lqw.like(StringUtils.isNotBlank(bo.getDrivewayName()), TbCarparking::getDrivewayName, bo.getDrivewayName());
        lqw.eq(bo.getEnterTime() != null, TbCarparking::getEnterTime, bo.getEnterTime());
        lqw.eq(StringUtils.isNotBlank(bo.getParkingNeed()), TbCarparking::getParkingNeed, bo.getParkingNeed());
        lqw.eq(StringUtils.isNotBlank(bo.getParkingNeed2()), TbCarparking::getParkingNeed2, bo.getParkingNeed2());
        lqw.eq(StringUtils.isNotBlank(bo.getFromIp()), TbCarparking::getFromIp, bo.getFromIp());
        lqw.orderByAsc(TbCarparking::getTrackId);
        lqw.orderBy(true, true, TbCarparking::getDrivewayName);
        return lqw;
    }

    /**
     * 新增存车场信息
     */
    @Override
    public Boolean insertByBo(TbCarparkingBo bo) {
        TbCarparking add = BeanUtil.toBean(bo, TbCarparking.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改存车场信息
     */
    @Override
    public Boolean updateByBo(TbCarparkingBo bo) {
        TbCarparking update = BeanUtil.toBean(bo, TbCarparking.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbCarparking entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除存车场信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TbCarparkingBo updateCarParking(TbCarparkingBo bo) {
        bo.setTrackId(null);
        bo.setTrackName(null);
        bo.setDrivewayName(null);
        if ((bo.getLmtType() != null && !bo.getLmtType().isEmpty()) && (bo.getLmtNumber() != null && !bo.getLmtNumber().isEmpty())) {
            Long lmtRepairId = tbRepairplanlistMapper.selectLmtId(bo.getLmtType(), bo.getLmtNumber());
            bo.setLmtRepairId(lmtRepairId);
            TbRepairplanlist planList = tbRepairplanlistMapper.selectPlanById(lmtRepairId);
            if (planList!=null){
                if (planList.getPlanName() != null) {
                    bo.setPlanName(planList.getPlanName());
                }
                if (planList.getStatus() != null) {
                    bo.setPlanStatus(planList.getStatus());
                }
                if (planList.getRepairType() != null) {
                    bo.setRepairType(planList.getRepairType());
                }
                if (planList.getWorkGroup() != null) {
                    bo.setWorkGroup(planList.getWorkGroup());
                }
                if (planList.getWorkGroupId() != null) {
                    bo.setWorkGroupId(planList.getWorkGroupId());
                }
            }
            return bo;
        } else {
            bo.setLmtRepairId(null);
            bo.setPlanName("");
            bo.setPlanStatus("");
            bo.setRepairType("");
            bo.setWorkGroup("");
            bo.setParkingNeed("");
            bo.setWorkGroupId(null);
            return bo;
        }
    }
}
